package com.lv.LvRefundRatioInfo.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aqgj.util.JsonWrapper;
import com.lv.LvRefundRatio.bean.LvRefundRatio;
import com.lv.LvRefundRatio.dao.LvRefundRatioDao;
import com.lv.LvRefundRatioInfo.bean.LvRefundRatioInfo;
import com.lv.LvRefundRatioInfo.dao.LvRefundRatioInfoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

@Service("LvRefundRatioInfo")
public class LvRefundRatioInfoManager {
    @Autowired
    LvRefundRatioInfoDao lvRefundRatioInfoDao;
    @Autowired
    LvRefundRatioDao lvRefundRatioDao;

    /**
     * 保存扣费比例设置.
     * @param lvRefundRatioInfo 扣费比例设置信息
     * @param lvRefundRatio  扣费比例详情json字符串
     * @return
     */
    @Transactional
    public HashMap<String,Object> saveInfo(LvRefundRatioInfo lvRefundRatioInfo, String lvRefundRatio){
        String id=null;
        if (lvRefundRatioInfo.getId() != null &&!"".equals(lvRefundRatioInfo.getId())) {
            id = lvRefundRatioInfoDao.update(lvRefundRatioInfo);
        } else {
            id = lvRefundRatioInfoDao.update(lvRefundRatioInfo);
        }
        if (id != null) {
            JSONArray jsonArray = JSON.parseArray(lvRefundRatio);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                LvRefundRatio lvRefundRatio1 = new LvRefundRatio();
                lvRefundRatio1.setFkLvRefundRatioInfoId(id);
                String ifRefund = jsonObject.get("isRefund").toString();
                //是否允许退票（1允许，2不允许）
                if ("1".equals(ifRefund)) {
                    lvRefundRatio1.setChargeScale(new BigDecimal(jsonObject.get("chargeScale").toString()));
                }
                lvRefundRatio1.setStartPoint(Integer.valueOf(jsonObject.get("startPoint").toString()));
                lvRefundRatio1.setIsRefund(ifRefund);
                lvRefundRatio1.setStatus(jsonObject.getInteger("status"));
                lvRefundRatio1.setEndPoint(Integer.valueOf(jsonObject.get("endPoint").toString()));
                lvRefundRatioDao.save(lvRefundRatio1);
            }
            return JsonWrapper.successWrapper("设置操作成功！");
        }
        return JsonWrapper.failureWrapperMsg("设置操作失败！");
    }

    /**
     * 删除
     * @param id 扣费比例设置id
     */
    public void removeById(String id) {
        lvRefundRatioInfoDao.removeById(id);
    }

    /**
     * 获取
     * @param id 扣费比例设置id
     * @return
     */
    public LvRefundRatioInfo get(String id) {
        return lvRefundRatioInfoDao.get(id);
    }

    /**
     * 保存
     * @param lvRefundRatioInfo 扣费比例设置对象
     * @return
     */
    public  String save(LvRefundRatioInfo lvRefundRatioInfo){
        return lvRefundRatioInfoDao.save(lvRefundRatioInfo);
    }
}
